<%
    require 'aboutyou-sdk'
    # To use the SDK you need to include the composer autoload file

    # Create an instance of the ShopAPI... Because the following queries will be quite
    # expensive, we use the ArrayCache. (4th argument). It's just stores every API
    # response in an array so it doesn't need to be done a second time. The array cache
    # will be lost after the request is done. That's why it's better to use a real
    # cache engine like Redis or MemCache.
    ay = AY.new(281, 'e6068c4ca020262613d06122663cb300')
    session_id = "sessionId123"

    # Get all facets from the API. This is quite expensive so if you really need this
    # more often you definitly should use a cache mechanism like memcache or Redis that
    # works greatly with the SDK.
    facets = ay.fetch_facets([AboutYou::SDK::Model::Facet::FACET_COLOR])

    # Now search for the color facet with the value "rot" to get the red color.
    # To have an overview over all the facets you can var_dump($facets).
    red = nil
    facets.each do |key, facet|
        if facet.value == 'rot'

            # The facet for the red color was found. You can hardcode it's ID and don't need
            # to search for this every time.
           red = facet
            break
        end
    end

    # Helper- Function for iterating over all Variant-Color-Facet-Models
    def iterate_over_variants(product, red, red_variants)
        product.variants.each do |key, variant|
          color = variant.facet_group(AboutYou::SDK::Model::Facet::FACET_COLOR)
          break if self.iterate_over_color(color, red, red_variants, product, variant)
      end
    end

    # Helper- Function for iterating over all Variant-Color-Facet-Models
    def iterate_over_color(color, red, red_variants, product, variant)
      color.facets.each do |key, facet|
        if facet.id == red.id
          red_variants.push({
            'product' => product,
            'variant' => variant
          })
          return true
        end
      end
      return false
    end

    # Let's search for products that have a product variant with red color by creating a
    # search criteria and pass it to the search method.
    criteria = ay.product_search_criteria(session_id)
    criteria.set_limit(25)
    criteria.select_product_fields([
        AboutYou::SDK::Criteria::ProductFields::VARIANTS
    ])
    criteria.filter_by_facet_ids([{ red.group_id => [red.id] }])

    product_result = ay.fetch_product_search(criteria)

    # Now iterate over every variant and check if it's white. If yes, we will place it in an array
    # to display it later.
    red_variants = []

    product_result.products.each do |key, product|
        self.iterate_over_variants(product, red, red_variants)
    end

    # Now list the variants with their image
%>

<ul>
    <% red_variants.each do |red_variant| %>
        <li>
            <%= red_variant['product'].name + '-' + String(red_variant['variant'].id) %>
            <% if red_variant['variant'].image %>
                <br />
                <img src="<%= red_variant['variant'].image.url(150,150) %>" alt="<%= red_variant['variant'].ean %>">
                </img>
            <% end %>
        </li>
    <% end %>
</ul>